import { createBrowserRouter, Navigate } from 'react-router-dom'
import AppLayout from '@/layouts/AppLayout'
import DashboardPage from '@/views/dashboard'
import LoginPage from '@/views/login'
import NotFoundPage from '@/views/error/NotFound'
import ErrorBoundaryPage from '@/views/error/ErrorBoundary'
import ProtectedRoute from './ProtectedRoute'
import PublicRoute from './PublicRoute'
import { DEFAULT_REDIRECT, ROUTES } from './paths'

const router = createBrowserRouter(
  [
    {
      path: ROUTES.root,
      element: <AppLayout />,
      errorElement: <ErrorBoundaryPage />,
      children: [
        {
          index: true,
          element: <Navigate to={DEFAULT_REDIRECT} replace />,
        },
        {
          path: ROUTES.dashboard,
          element: (
            <ProtectedRoute>
              <DashboardPage />
            </ProtectedRoute>
          ),
        },
      ],
    },
    {
      path: ROUTES.login,
      element: (
        <PublicRoute>
          <LoginPage />
        </PublicRoute>
      ),
    },
    {
      path: '*',
      element: <NotFoundPage />,
    },
  ],
  {
    basename: import.meta.env.BASE_URL,
  },
)

export default router
